package com.onlyxiahui.common.action.description.handler.impl.result;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import com.onlyxiahui.common.action.description.DocumentContext;
import com.onlyxiahui.common.action.description.bean.MethodData;
import com.onlyxiahui.common.action.description.bean.ModuleData;
import com.onlyxiahui.common.action.description.bean.ResultData;
import com.onlyxiahui.common.action.description.enums.annotation.MethodRequest;
import com.onlyxiahui.common.action.description.enums.annotation.MethodResponse;
import com.onlyxiahui.common.action.description.enums.annotation.ModuleMapping;
import com.onlyxiahui.common.action.description.handler.impl.BaseResultHandler;
import com.onlyxiahui.common.action.description.util.ActionAnnotationUtil;

/**
 * Description <br>
 * Date 2020-01-07 16:17:59<br>
 * 
 * @author XiaHui [onlovexiahui@qq.com]<br>
 * @since 1.0.0
 */

public class DefaultResultHandler extends BaseResultHandler {

	@Override
	public boolean support(DocumentContext context, ModuleData moduleData, MethodData methodData, Method method) {
		Class<?> classType = method.getDeclaringClass();
		Annotation[] cas = classType.getAnnotations();
		boolean hasRestController = ActionAnnotationUtil.hasAnnotation(ModuleMapping.RestController.getAnnotation(), cas);
		Annotation[] mas = method.getAnnotations();
		boolean hasActionMapping = ActionAnnotationUtil.hasAnnotation(MethodRequest.ActionMapping.getAnnotation(), mas);
		boolean hasResponseBody = ActionAnnotationUtil.hasAnnotation(MethodResponse.ResponseBody.getAnnotation(), mas);
		return hasRestController || hasActionMapping || hasResponseBody;
	}

	@Override
	public ResultData handle(DocumentContext context, ModuleData moduleData, MethodData methodData, Method method) {
		return create(context, moduleData, methodData, method);
	}
}
